//
//  ChouTiViewController.m
//  CookGuest
//
//  Created by dllo on 15/9/18.
//  Copyright (c) 2015年 杨元石. All rights reserved.
//

#import "ChouTiViewController.h"
#import "LeftSlideViewController.h"


@interface ChouTiViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UISwitch * open;
@property (nonatomic, retain)UIAlertView * alertView;

@end

@implementation ChouTiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    UIImageView *backImage = [[UIImageView alloc] initWithFrame:self.view.frame];
    backImage.image = [UIImage imageNamed:@"chouti.png"];
    [self.view addSubview:backImage];
    
    UILabel * header = [[UILabel alloc] init];
    header.backgroundColor = [UIColor clearColor];
//    header.text = @"Header View";
    header.textAlignment = NSTextAlignmentCenter;
    header.font = [UIFont boldSystemFontOfSize:25];
    header.textColor = [UIColor whiteColor];
    header.frame = CGRectMake(0, 0, ADJUSTWIDTH*375, ADJUSTHEIGHT*200);
    
   
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, ADJUSTHEIGHT*200, ADJUSTWIDTH*375, ADJUSTHEIGHT*200) style:UITableViewStylePlain];
    _tableView.separatorColor = [UIColor clearColor];
    _tableView.delegate = self;
    _tableView.dataSource =self;
    _tableView.tableHeaderView = header;
    [_tableView setBounces:NO];
    [self.view addSubview:_tableView];
    
    // Do any additional setup after loading the view.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *cellId = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    
    if (nil == cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    //-------
    if (indexPath.row == 2) {
        UISwitch * Switch = [[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
//        Switch.backgroundColor = [UIColor greenColor];
        Switch.center = CGPointMake(ADJUSTWIDTH*230, cell.contentView.frame.size.height/2);
        [cell.contentView addSubview:Switch];
        
        [Switch addTarget:self action:@selector(Switch1Tag:) forControlEvents:UIControlEventValueChanged];
    }
    
    //-------
    NSArray *arr = @[@"我的收藏", @"清除缓存", @"夜间模式", @"关于我们"];
    UIImage *img1 = [UIImage imageNamed:@"wodeshoucang.png"];
    UIImage *img2 = [UIImage imageNamed:@"qinghuan.png"];
    UIImage *img3 = [UIImage imageNamed:@"yejian.png"];
    UIImage *img4 = [UIImage imageNamed:@"guanyu.png"];
    
    NSArray * imgArr = @[img1,img2,img3,img4];
    cell.textLabel.text = arr[indexPath.row];
    cell.imageView.image = imgArr[indexPath.row];
    cell.textLabel.font = [UIFont boldSystemFontOfSize:20];
    cell.backgroundColor = [UIColor clearColor];
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (indexPath.row == 0) {
        NSLog(@"我的收藏");
        SecViewController *minVc = [[SecViewController alloc] init];
        LeftSlideViewController *yrs =  (LeftSlideViewController *) [[[[UIApplication sharedApplication]delegate]window]rootViewController];
        UITabBarController *tab  = (UITabBarController *)yrs.mainVC;
        [yrs closeLeftView];
        UINavigationController *navi111 = (UINavigationController *)tab.selectedViewController;
        [navi111 pushViewController:minVc animated:YES];
        
        
        
        
    } else if (indexPath.row == 1) {
     //----
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"提醒" message:@"是否清理缓存" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alert setTag:1000];
        [alert show];
        //-----
        
        
        
        NSLog(@"清除缓存");
        
    } else if (indexPath.row == 2) {
        
        [[SDImageCache sharedImageCache] clearDisk];
        [self.tableView reloadData];
        
        NSLog(@"夜间模式");
    } else if (indexPath.row == 3) {
        
        self.alertView = [[UIAlertView alloc]initWithTitle:@"关于CookGuest" message:@"\n开发者邮箱:\n 13840866026@163.com\n欢迎提出宝贵意见" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [self.alertView show];
        
        
        NSLog(@"关于我们");
        
    }
    
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag==1000) {
        switch (buttonIndex){
            case 0:
            // NSLog(@"取消");
                break;
            case 1:
                [self clearPics];
            // NSLog(@"确定");
            default:
                break;
        }
    }
}
// 清理缓存
- (void)clearPics
{
    float tmpSize = [[SDImageCache sharedImageCache] getSize];
    NSString * clearCacheName = tmpSize >= 1024 * 1024 ? [NSString stringWithFormat:@"清理缓存(%.2fM)" , tmpSize / 1024 / 1024] : [NSString stringWithFormat:@"清理缓存(%.2fK)", tmpSize / 1024];
    UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"提醒" message:clearCacheName delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    [alertView show];
    [[SDImageCache sharedImageCache]clearDisk];
    //清除内存缓存
    [[[SDWebImageManager sharedManager] imageCache] clearMemory];
    //清除系统缓存
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
}


// 夜间模式实现
-(void)Switch1Tag:(UISwitch *)Switch
{
    if (Switch.on) {
        self.view.window.alpha = 0.418;
    }else{
        self.view.window.alpha = 1;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
